/**
 * 本地存储管理模块
 */
const Storage = {
    // 存储键名常量
    keys: {
        DEVICE_ID: 'device_id',
        FAVORITES: 'favorites',
        PREFERENCES: 'preferences',
        RECENTLY_VIEWED: 'recently_viewed',
        SEARCH_HISTORY: 'search_history'
    },
    
    // 获取设备ID
    getDeviceId() {
        return localStorage.getItem(this.keys.DEVICE_ID);
    },
    
    // 存储设备ID
    setDeviceId(deviceId) {
        localStorage.setItem(this.keys.DEVICE_ID, deviceId);
    },
    
    // 获取收藏列表
    getFavorites() {
        return JSON.parse(localStorage.getItem(this.keys.FAVORITES) || '[]');
    },
    
    // 保存收藏列表
    saveFavorites(favorites) {
        localStorage.setItem(this.keys.FAVORITES, JSON.stringify(favorites));
    },
    
    // 添加到收藏
    addFavorite(wallpaperId) {
        const favorites = this.getFavorites();
        if (!favorites.includes(wallpaperId)) {
            favorites.push(wallpaperId);
            this.saveFavorites(favorites);
        }
    },
    
    // 从收藏中移除
    removeFavorite(wallpaperId) {
        const favorites = this.getFavorites();
        const index = favorites.indexOf(wallpaperId);
        if (index !== -1) {
            favorites.splice(index, 1);
            this.saveFavorites(favorites);
        }
    },
    
    // 检查是否收藏
    isFavorite(wallpaperId) {
        const favorites = this.getFavorites();
        return favorites.includes(wallpaperId);
    },
    
    // 获取偏好设置
    getPreferences() {
        return JSON.parse(localStorage.getItem(this.keys.PREFERENCES) || '{}');
    },
    
    // 保存偏好设置
    savePreferences(preferences) {
        localStorage.setItem(this.keys.PREFERENCES, JSON.stringify(preferences));
    },
    
    // 更新单个偏好设置
    updatePreference(key, value) {
        const preferences = this.getPreferences();
        preferences[key] = value;
        this.savePreferences(preferences);
    },
    
    // 获取最近查看列表
    getRecentlyViewed() {
        return JSON.parse(localStorage.getItem(this.keys.RECENTLY_VIEWED) || '[]');
    },
    
    // 添加到最近查看
    addRecentlyViewed(wallpaperId) {
        const recentlyViewed = this.getRecentlyViewed();
        // 如果已存在，先移除旧记录
        const index = recentlyViewed.indexOf(wallpaperId);
        if (index !== -1) {
            recentlyViewed.splice(index, 1);
        }
        // 添加到开头
        recentlyViewed.unshift(wallpaperId);
        // 限制数量，最多保存20条
        if (recentlyViewed.length > 20) {
            recentlyViewed.pop();
        }
        localStorage.setItem(this.keys.RECENTLY_VIEWED, JSON.stringify(recentlyViewed));
    },
    
    // 获取搜索历史
    getSearchHistory() {
        return JSON.parse(localStorage.getItem(this.keys.SEARCH_HISTORY) || '[]');
    },
    
    // 添加搜索历史
    addSearchHistory(query) {
        if (!query) return;
        
        const history = this.getSearchHistory();
        // 如果已存在，先移除旧记录
        const index = history.indexOf(query);
        if (index !== -1) {
            history.splice(index, 1);
        }
        // 添加到开头
        history.unshift(query);
        // 限制数量，最多保存10条
        if (history.length > 10) {
            history.pop();
        }
        localStorage.setItem(this.keys.SEARCH_HISTORY, JSON.stringify(history));
    },
    
    // 清除搜索历史
    clearSearchHistory() {
        localStorage.removeItem(this.keys.SEARCH_HISTORY);
    },
    
    // 清除所有数据
    clearAll() {
        localStorage.clear();
    }
};

// 导出模块
if (typeof module !== 'undefined' && module.exports) {
    module.exports = Storage;
} 